package es.udc.fic.hamNeggs.manager;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.udc.fic.hamNeggs.pojo.Sprint;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Repository(value = "sprint")
public class SprintManager extends AbstractManager {
    private final static Logger LOG = Logger.getLogger(SprintManager.class.getName());

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public List<Sprint> getSprintList() {
        List<Sprint> list = session.createQuery("from Sprint").list();

        Collections.sort(list,
                new Comparator<Sprint>() {
                    public int compare(Sprint o1, Sprint o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return list;
    }

    @Transactional(readOnly = true)
    public Sprint getSprint(Long id) {
        Sprint spr = (Sprint) session.get(Sprint.class, id);
        return spr;
    }

    @Transactional(readOnly = false)
    public void saveSprint(Sprint sprint) {
        super.save(sprint, LOG);
    }

    @Transactional(readOnly = false)
    public void deleteSprint(Sprint sprint) {
        super.delete(sprint, LOG);
    }

    @Transactional(readOnly = false)
    public void updateSprint(Sprint sprint) {
        super.update(sprint, LOG);
    }

}
